<?php
require_once("phpmailer/class.phpmailer.php");
		
class retrieve_reminder_data_model extends CI_Model {

	public function __construct()
	{
		parent::__construct();
	}
	
	public function sendviaemail($data){
		$mail = new PHPMailer();
		$mail->IsSMTP(); // send via SMTP
		//IsSMTP(); // send via SMTP
		$mail->SMTPAuth = true; // turn on SMTP authentication
		$mail->Username = "reminder.system.ufl"; // SMTP username
		$mail->Password = "10nlys3ndRem1nd3rs"; // SMTP password
		$webmaster_email = "reminder.system.ufl@gmail.com"; //Reply to this email ID
		$email=$data['email']; // Recipients email ID
		$name="Sonia qwerty"; // Recipient's name
		$mail->From = $webmaster_email;
		$mail->FromName = "Reminder System";
		$mail->AddAddress($email,$name); 
		$mail->AddReplyTo($webmaster_email,"Reminder System");
		$mail->WordWrap = 50; // set word wrap
		//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
		//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
		$mail->IsHTML(true); // send as HTML
		$mail->Subject = "Your Reminder Alert";
		$mail->Body = "TASK: ".$data['task'] . 
		"<br/>DATE: ".$data['scheduled_date'].
		"<br/>TIME: ".$data['scheduled_time'];
		//HTML Body
		$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
		if(!$mail->Send()){
			echo "Mailer Error: " . $mail->ErrorInfo;
		}
		else{
			echo "Message has been sent";
		}

	}
	public function sendviaphone($data){
	}
	public function retrieve_data()
	{
		$retrieved_data_array = array();
		$email=""; $phone=""; $task="";
		$a = localtime(time(),true);
		$starttime = $a['tm_hour'].":".$a['tm_min'].":".$a['tm_sec'];
		
		$a = localtime(time()+1800,true);
		$endtime = $a['tm_hour'].":".$a['tm_min'].":".$a['tm_sec'];
		
		echo $starttime;
		echo $endtime;
		// find all the reminders scheduled within the next 30 mins
		$this->db->where('scheduled_date',date("Y-m-d"));
		$this->db->where('scheduled_time <', $endtime);
		$this->db->where('scheduled_time >', $starttime);
		$rs_scheduled_reminders = $this->db->get('scheduled_reminders');
		
		echo "woo";
		echo $rs_scheduled_reminders->num_rows();
		//get task and notification type from reminder table
		if($rs_scheduled_reminders->num_rows() > 0) {
			foreach ($rs_scheduled_reminders->result() as $resultitem_schrem) {
				
				$reminder_id = $resultitem_schrem->id;
				$retrieved_data_array['reminder_date'] = $resultitem_schrem->scheduled_date;
				$retrieved_data_array['reminder_time'] = $resultitem_schrem->scheduled_time;
				echo "<br/>reminder id: ".$reminder_id."<br/>";
				$this->db->where('id',$reminder_id);
				$this->db->select('user_id, task, is_email, is_phone');
				
				$rs_reminders = $this->db->get('reminder');
				foreach($rs_reminders->result() as $resultitem_rem){
					$this->db->where('id',$resultitem_rem->user_id);
					echo "<br/>user id: ".$resultitem_rem->user_id."<br/>";
					$notification_type="";
					$retrieved_data_array['task']=$resultitem_rem->task;
					
					if($resultitem_rem->is_email && $resultitem_rem->is_phone){
						echo "yay";
						//go to user table and get email id
						$this->db->select('email,phone');
						$notification_type = "both";
					}
					else if($resultitem_rem->is_phone){
						$this->db->select('phone');
						$notification_type="phone";
					}
					else if($resultitem_rem->is_email){
						$this->db->select('email');
						$notification_type="email";
					}
					echo "<br/>notification type: ".$notification_type."<br/>";
					$rs_user = $this->db->get('user');
					echo $rs_user->num_rows();
					
					
					foreach($rs_user->result() as $resultitem_usr){
						if($notification_type == "both"){
							echo "both ep";
							$retrieved_data_array['email'] = $resultitem_usr->email;
							$retrieved_data_array['phone'] = $resultitem_usr->phone;
							
							$mail = new PHPMailer();
							$mail->IsSMTP(); // send via SMTP
							//IsSMTP(); // send via SMTP
							$mail->SMTPAuth = true; // turn on SMTP authentication
							$mail->Username = "reminder.system.ufl"; // SMTP username
							$mail->Password = "10nlys3ndRem1nd3rs"; // SMTP password
							$webmaster_email = "reminder.system.ufl@gmail.com"; //Reply to this email ID
							$email=$retrieved_data_array['email']; // Recipients email ID
							$name="Sonia qwerty"; // Recipient's name
							$mail->From = $webmaster_email;
							$mail->FromName = "Reminder System";
							$mail->AddAddress($email,$name); 
							$mail->AddReplyTo($webmaster_email,"Reminder System");
							$mail->WordWrap = 50; // set word wrap
							//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
							//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
							$mail->IsHTML(true); // send as HTML
							$mail->Subject = "Your Reminder Alert";
							$mail->Body = "TASK: ".$retrieved_data_array['task'] . 
							"<br/>DATE: ".$retrieved_data_array['scheduled_date'].
							"<br/>TIME: ".$retrieved_data_array['scheduled_time'];
							//HTML Body
							$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
							if(!$mail->Send()){
								echo "Mailer Error: " . $mail->ErrorInfo;
							}
							else{
								echo "Message has been sent";
							}
		
							sendviaphone($retrieved_data_array);
						}
						else if($notification_type == "email"){
							$retrieved_data_array['email'] = $resultitem_usr->email;
							
							$mail = new PHPMailer();
							$mail->IsSMTP(); // send via SMTP
							//IsSMTP(); // send via SMTP
							$mail->SMTPAuth = true; // turn on SMTP authentication
							$mail->Username = "reminder.system.ufl"; // SMTP username
							$mail->Password = "10nlys3ndRem1nd3rs"; // SMTP password
							$webmaster_email = "reminder.system.ufl@gmail.com"; //Reply to this email ID
							$email=$retrieved_data_array['email']; // Recipients email ID
							$name="Sonia qwerty"; // Recipient's name
							$mail->From = $webmaster_email;
							$mail->FromName = "Reminder System";
							$mail->AddAddress($email,$name); 
							$mail->AddReplyTo($webmaster_email,"Reminder System");
							$mail->WordWrap = 50; // set word wrap
							//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
							//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
							$mail->IsHTML(true); // send as HTML
							$mail->Subject = "Your Reminder Alert";
							$mail->Body = "TASK: ".$retrieved_data_array['task'] . 
							"<br/>DATE: ".$retrieved_data_array['scheduled_date'].
							"<br/>TIME: ".$retrieved_data_array['scheduled_time'];
							//HTML Body
							$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
							if(!$mail->Send()){
								echo "Mailer Error: " . $mail->ErrorInfo;
							}
							else{
								echo "Message has been sent";
							}
						}
						else{
							$retrieved_data_array['phone'] = $resultitem_usr->phone;
							sendviaphone($retrieved_data_array);
						}
					}
										
				}
			}	
		}
	}
}	
?>